#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<stack>
#include<unordered_set>

using namespace std;

typedef pair<int, int> PII;

//bool canMeasureWater(int x, int y, int z) {
//    if (x + y < z)   return false;
//    if (x == 0 || y == 0)    return z == 0 || x + y == z;
//    return z % gcd(x, y) == 0;
//}

bool canMeasureWater(int x, int y, int z) {
    stack<PII> stk;
    stk.emplace(0, 0);
    auto hashf = [](const PII& o) {
        return hash<int>()(o.first) ^ hash<int>()(o.second);
    };
    unordered_set<PII, decltype(hashf)> st(0, hashf);
    while (stk.size())
    {
        if (st.count(stk.top()))
        {
            stk.pop();
            continue;
        }
        st.emplace(stk.top());
        auto [rx, ry] = stk.top();
        stk.pop();
        if (rx == z || ry == z || rx + ry == z)  return true;

        stk.emplace(x, ry);
        stk.emplace(rx, y);
        stk.emplace(0, ry);
        stk.emplace(rx, 0);
        stk.emplace(rx - min(rx, y - ry), ry + min(rx, y - ry));
        stk.emplace(rx + min(ry, x - rx), ry - min(ry, x - rx));
    }
    return false;
}